package com.hairline.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hairline.bean.*;
import com.hairline.bean.vo.CommentDataVo;
import com.hairline.bean.vo.Floor;
import com.hairline.bean.vo.GroupListVo;
import com.hairline.bean.vo.SpecificationVo;
import com.hairline.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

@Service
public class WxIndexServiceImpl implements WxIndexService{

    @Autowired
    GoodsMapper goodsMapper;
    @Autowired
    CouponMapper couponMapper;
    @Autowired
    CategoryMapper categoryMapper;
    @Autowired
    GrouponRulesMapper grouponRulesMapper;
    @Autowired
    AdMapper adMapper;
    @Autowired
    BrandMapper brandMapper;
    @Autowired
    TopicMapper topicMapper;
    @Autowired
    GoodsSpecificationMapper goodsSpecificationMapper;
    @Autowired
    GrouponMapper grouponMapper;
    @Autowired
    IssueMapper issueMapper;
    @Autowired
    CollectMapper collectMapper;
    @Autowired
    CommentMapper commentMapper;
    @Autowired
    GoodsAttributeMapper goodsAttributeMapper;
    @Autowired
    GoodsProductMapper goodsProductMapper;
    @Autowired
    SystemMapper systemMapper;
    @Autowired
    SearchHistoryMapper searchHistoryMapper;

    @Override
    public Map<String, Object> queryGoodsList() {
        Map<String, Object>  map = new HashMap<>();

        //新品
        GoodsExample goodsExample = new GoodsExample();
        GoodsExample.Criteria criteria = goodsExample.createCriteria();
        criteria.andIsNewEqualTo(true);
        //查出系统表中对应的value
        int newGood = getKeyValue("cskaoyanmall_wx_index_new");
        PageHelper.offsetPage(0,newGood);
        List<Goods> newGoods = goodsMapper.selectByExample(goodsExample);
        map.put("newGoodsList",newGoods);

        //人气推荐
        GoodsExample goodsExample1 = new GoodsExample();
        GoodsExample.Criteria criteria1 = goodsExample1.createCriteria();
        criteria1.andIsHotEqualTo(true);
        int hot = getKeyValue("cskaoyanmall_wx_index_hot");
        PageHelper.offsetPage(0,hot);
        List<Goods> hotlist = goodsMapper.selectByExample(goodsExample1);
        map.put("hotGoodsList",hotlist);

        //couponList
        PageHelper.offsetPage(0,4);
        List<Coupon> coupons = couponMapper.selectByExample(null);
        map.put("couponList",coupons);

        //channel
        CategoryExample categoryExample = new CategoryExample();
        CategoryExample.Criteria criteria2 = categoryExample.createCriteria();
        criteria2.andPidEqualTo(0);
        List<Category> categories = categoryMapper.selectByExample(categoryExample);
        map.put("channel",categories);

        //grouponList
        PageHelper.offsetPage(0,4);
        List<GrouponRules> grouponRules = grouponRulesMapper.selectByExample(null);
        List<GroupListVo> groupListVos= new ArrayList<>();
        for (GrouponRules grouponRule : grouponRules) {
            Integer goodsId = grouponRule.getGoodsId();
            Goods good = goodsMapper.selectByPrimaryKey(goodsId);
            //拿到了good  还差土狗价格 团购人数  TD
            List<GroupListVo> groupListVos1 = grouponRulesMapper.selectPrice(goodsId);
            BigDecimal retailPrice = good.getRetailPrice();
            for (GroupListVo groupListVo : groupListVos1) {
                int groupPrice = retailPrice.intValue() - groupListVo.getGroupon_price();
                groupListVo.setGroupon_price(groupPrice);
                groupListVo.setGoods(good);
                groupListVos.add(groupListVo);
            }
        }
        map.put("grouponList",groupListVos);

        //banner
        List<Ad> ads = adMapper.selectByExample(null);
        map.put("banner",ads);

        //brandList
        int brand = getKeyValue("cskaoyanmall_wx_index_brand");
        PageHelper.offsetPage(0,brand);
        List<Brand> brands = brandMapper.selectByExample(null);
        map.put("brandList",brands);

        //topicList
        int topic = getKeyValue("cskaoyanmall_wx_index_topic");
        PageHelper.offsetPage(0,topic);
        List<Topic> topics = topicMapper.selectByExample(null);
        map.put("topicList",topics);

        //floorGoodsList
        List<Floor> floors = new ArrayList<>();
        //List<Goods> good1 = new ArrayList<>();
       // List<Category> categories1 = categoryMapper.selectByExample(null);
        CategoryExample categoryExample1 = new CategoryExample();
        CategoryExample.Criteria criteria0 = categoryExample1.createCriteria();
        criteria0.andPidEqualTo(0);
        int list = getKeyValue("cskaoyanmall_wx_catlog_list");
        PageHelper.offsetPage(0,list);
        List<Category> categories1 = categoryMapper.selectByExample(categoryExample1);
        int goodsList = getKeyValue("cskaoyanmall_wx_catlog_goods");
        for (Category category : categories1) {
            Floor floor = new Floor();
            floor.setName(category .getName());
            Integer id = category .getId();
            floor.setId(id);
            CategoryExample categoryExamples = new CategoryExample();
            CategoryExample.Criteria criterias = categoryExamples.createCriteria();
            criterias.andPidEqualTo(id);
            List<Category> categories2 = categoryMapper.selectByExample(categoryExamples);
            Integer id1 = categories2.get(0).getId();
            PageHelper.offsetPage(0,goodsList);
            List<Goods> goods = goodsMapper.selectGoods(id1);
            floor.setGoodsList(goods);
            floors.add(floor);
        }
        map.put("floorGoodsList",floors);
        return map;
    }

    private int getKeyValue(String s) {
        String value = systemMapper.selectKeyValue(s);
        int i = Integer.parseInt(value);
        return i;
    }

    @Override
    public GoodsCount queryCount() {
        List<Goods> goods = goodsMapper.selectByExample(null);
        PageInfo<Goods> pageInfo = new PageInfo<Goods>(goods);//在pageInfo中放入查询结果
        long total = pageInfo.getTotal();
        GoodsCount goodsCount = new GoodsCount();
        goodsCount.setGoodsCount( total);
        return goodsCount;
    }

    @Override
    public Map<String, Object> queryCategory(Integer id) {
        Map<String, Object> map = new HashMap<>();
        CategoryExample categoryExample = new CategoryExample();
        CategoryExample.Criteria criteria = categoryExample.createCriteria();
        criteria.andPidEqualTo(id);
        List<Category> categories = categoryMapper.selectByExample(categoryExample);
        Category category = categoryMapper.selectByPrimaryKey(id);
        if (category.getPid() == 0) {
            map.put("parentCategory",category);
            map.put("currentCategory",categories.get(0));
            map.put("brotherCategory",categories);
        }else {
            Integer pid = category.getPid();
            Category category1 = categoryMapper.selectByPrimaryKey(pid);
            map.put("parentCategory",category1);
            map.put("currentCategory",category);
            CategoryExample categoryExample1 = new CategoryExample();
            CategoryExample.Criteria criteria1 = categoryExample1.createCriteria();
            criteria1.andPidEqualTo(pid);
            List<Category> categories1 = categoryMapper.selectByExample(categoryExample1);
            map.put("brotherCategory",categories1);
        }
        //Category category = categoryMapper.selectByPrimaryKey(id);
        //map.put("parentCategory",category);
        return map;
    }

    @Override
    public Map<String, Object> queryList(Integer categoryId, Integer page, Integer size) {
        Map<String, Object> map = new HashMap<>();
        GoodsExample goodsExample = new GoodsExample();
        GoodsExample.Criteria criteria = goodsExample.createCriteria();
        criteria.andCategoryIdEqualTo(categoryId);
        if (page != null && size != null) {
            PageHelper.startPage(page, size);
        }
        List<Goods> goods = goodsMapper.selectByExample(goodsExample);
        int size1 = goods.size();
        map.put("count",size1);
        map.put("goodsList",goods);

        //filterCategoryList
        CategoryExample categoryExample = new CategoryExample();
        categoryExample.createCriteria().andPidNotEqualTo(0);
        List<Category> categories = categoryMapper.selectByExample(categoryExample);
        //map.put("filterCategoryList",categories);
        return map;
    }


    @Override
    public Map<String, Object> goodsDetail(Integer id) {
        HashMap<String, Object> map = new HashMap<>();

        //specificationList
        List<SpecificationVo> specificationVos = new ArrayList<>();
        SpecificationVo specificationVo = new SpecificationVo();
        GoodsSpecificationExample goodsSpecificationExample = new GoodsSpecificationExample();
        GoodsSpecificationExample.Criteria criteria = goodsSpecificationExample.createCriteria();
        criteria.andGoodsIdEqualTo(id);
        List<GoodsSpecification> goodsSpecifications = goodsSpecificationMapper.selectByExample(goodsSpecificationExample);
        String specification = goodsSpecifications.get(0).getSpecification();
        specificationVo.setName(specification);
        specificationVo.setValueList(goodsSpecifications);
        specificationVos.add(specificationVo);
        map.put("specificationList",specificationVos);

        //groupon
        GrouponRulesExample grouponExample = new GrouponRulesExample();
        grouponExample.createCriteria().andGoodsIdEqualTo(id);
        List<GrouponRules> grouponRules = grouponRulesMapper.selectByExample(grouponExample);
        map.put("groupon",grouponRules);

        //issue
        List<Issue> issues = issueMapper.selectByExample(null);
        map.put("issue",issues);
        CollectExample collectExample = new CollectExample();
        collectExample.createCriteria().andValueIdEqualTo(id);
        List<Collect> collects = collectMapper.selectByExample(collectExample);
        if (collects.size() == 0) {
            map.put("userHasCollect",0);
        }else {
            map.put("userHasCollect",collects.size());
        }

        //shareImage
        Goods goods = goodsMapper.selectByPrimaryKey(id);
        map.put("shareImage",goods.getShareUrl());

        //comment
        CommentExample commentExample = new CommentExample();
        commentExample.createCriteria().andValueIdEqualTo(id);
        List<Comment> comments = commentMapper.selectByExample(commentExample);
        CommentDataVo commentDataVo = new CommentDataVo();
        commentDataVo.setCount(comments.size());
        List<String> objects = new ArrayList<>();
        for (Comment comment : comments) {
            objects.add(comment.getContent());
        }
        commentDataVo.setData(objects);
        map.put("comment",commentDataVo);

        //attribute
        GoodsAttributeExample goodsAttributeExample = new GoodsAttributeExample();
        goodsAttributeExample.createCriteria().andGoodsIdEqualTo(id);
        List<GoodsAttribute> goodsAttributes = goodsAttributeMapper.selectByExample(goodsAttributeExample);
        map.put("attribute",goodsAttributes);

        //brand
        Integer brandId = goods.getBrandId();
        Brand brand = brandMapper.selectByPrimaryKey(brandId);
        map.put("brand", brand);

        //productList
        GoodsProductExample goodsProductExample = new GoodsProductExample();
        goodsProductExample.createCriteria().andGoodsIdEqualTo(id);
        List<GoodsProduct> goodsProducts = goodsProductMapper.selectByExample(goodsProductExample);
        map.put("productList",goodsProducts);

        //info
        map.put("info",goods);
        return  map;
    }

    @Override
    public List<Goods> goodsRelate(Integer id) {
        Goods goods1 = goodsMapper.selectByPrimaryKey(id);
        Integer categoryId = goods1.getCategoryId();
        GoodsExample goodsExample = new GoodsExample();
        goodsExample.createCriteria().andCategoryIdEqualTo(categoryId);
        List<Goods> goods = goodsMapper.selectByExample(goodsExample);
        return goods;
    }

    @Override
    public Map<String, Object> queryLists(String keyword, Integer page, Integer size, String sort, String order, Integer categoryId) {
        Map<String, Object> map = new HashMap<>();

        //goodsList
        GoodsExample goodsExample = new GoodsExample();
        GoodsExample.Criteria criteria = goodsExample.createCriteria();
        if (categoryId != 0){
           criteria.andCategoryIdEqualTo(categoryId);
        }else  criteria.andNameLike("%" + keyword + "%");
        goodsExample.setOrderByClause(sort + " "+ order);
        if (page != null && size != null) {
            PageHelper.startPage(page, size);
        }
        List<Goods> goods = goodsMapper.selectByExample(goodsExample);

        //filterCategoryList
        Set set = new HashSet();
        for (Goods good : goods) {
            Integer categoryId1 = good.getCategoryId();
            set.add(categoryId1);
        }
        List list = new ArrayList(set);
        CategoryExample categoryExample = new CategoryExample();
        categoryExample.createCriteria().andIdIn(list);
        List<Category> categories = categoryMapper.selectByExample(categoryExample);
        map.put("filterCategoryList",categories);

        int size1 = goods.size();
        map.put("count",size1);
        map.put("goodsList",goods);

        //加入历史搜索记录
        SearchHistory searchHistory = new SearchHistory();
        searchHistory.setAddTime(new Date());
        searchHistory.setKeyword(keyword);
        searchHistory.setUserId(1);
        searchHistory.setUpdateTime(new Date());
        searchHistory.setDeleted(false);
        searchHistory.setFrom("wx");
        searchHistoryMapper.insertSelective(searchHistory);
        return map;
    }
}
